package com.common.Utils;

import java.security.MessageDigest;

public class MD5Util {
    private static final String SALT = "your_salt_value";

    public static String encrypt(String password) {
        try {
            // 加盐
            String saltPassword = password + SALT;
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = md.digest(saltPassword.getBytes("UTF-8"));
            return toHex(bytes);
        } catch (Exception e) {
            throw new RuntimeException("MD5加密出错", e);
        }
    }

    private static String toHex(byte[] bytes) {
        StringBuilder ret = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) {
            ret.append(String.format("%02x", b));
        }
        return ret.toString();
    }
}